package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.AuthorISBN;
import cn.jhc.bean.Title;
import cn.jhc.factory.ConnectionFactory;

public class AuthorIsbnDAO {
	public List<AuthorISBN> getAll(){
		List<AuthorISBN> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "select authorId,isbn from authorISBN";
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				AuthorISBN authorisbn = new AuthorISBN();
				authorisbn.setAuthorID(rs.getInt(1));
				authorisbn.setIsbn(rs.getString(2));
				list.add(authorisbn);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public List findByID(int id) {
		List<AuthorISBN> list = new ArrayList();
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		final String sql = "select AuthorID,isbn from authorISBN where authorID=?"; 
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1,id);
			rs = ps.executeQuery();
			
			while(rs.next()) {
				AuthorISBN authorisbn = new AuthorISBN();
				authorisbn.setAuthorID(rs.getInt(1));
				authorisbn.setIsbn(rs.getString(2));
				list.add(authorisbn);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				rs.close();
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public void deleteAuthorisbn(int id){
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		final String sql = "delete authorISBN where authorID = ?"; 
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1,id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void addAuthorIsbn(AuthorISBN authorisbn){
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		final String sql = "insert into authorisbn values(?,?)"; 
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1,authorisbn.getAuthorID());
			ps.setString(2,authorisbn.getIsbn());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}